#!/usr/bin/env python
#coding: utf-8
'''
Created on 2017年2月22日

@author: Administrator
'''
from account.models import  Group, UserGroup, Profile
from django.contrib import admin

class GroupAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'parent', 'remark')
    list_filter = ('state',)
    list_per_page = 50
    raw_id_fields = ('parent',)
    search_fields = ('name',)
    
class UserGroupAdmin(admin.ModelAdmin):
    list_display = ('id', 'user', 'group', 'is_admin')
    list_filter = ('state',)
    list_per_page = 50
    raw_id_fields = ('user', 'group')
    search_fields = ('user__username', 'group__name')
    
class ProfileAdmin(admin.ModelAdmin):
    
    def queryset(self, request):
        profiles = super(ProfileAdmin, self).queryset(request)
        
        if request.GET.has_key('all'):
            all_params = request.GET.get('all')
            params = all_params.split('|')
            if params[0] == '1':
                profiles = profiles.filter(user__designer__isnull=False)
                
            if len(params) > 1:
                if params[1]:
                    profiles = profiles.filter(create_time__gt=params[1])
                if params[2]:
                    profiles = profiles.filter(create_time__lt=params[2])
                
        return profiles
    
    list_display = ('user', 'utype', 'user_realname', 'mobile', 'mobile_authed', 'user_last_login', 'user_date_joined')
    raw_id_fields = ('user',)
    search_fields = ('user__email', 'user__last_name', 'mobile')
    list_filter = ('utype', 'email_authed', 'mobile_authed', 'identity_authed', 'state', 'user__is_staff', 'user__is_superuser')
    list_per_page = 10
    
    def user_realname(self, obj):
        return obj.user.last_name
    user_realname.short_description = u'真实姓名'
    
    def user_last_login(self, obj):
        return obj.user.last_login
    user_last_login.short_description = u'最后一次登录时间'
    
    def user_date_joined(self, obj):
        return obj.user.date_joined
    user_date_joined.short_description = u'注册日期'

admin.site.register(Profile, ProfileAdmin)
admin.site.register(Group, GroupAdmin)
admin.site.register(UserGroup, UserGroupAdmin)
